package com.junyidao.game.controller;

import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.constant.SysUserConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.core.annotation.OperLog;
import com.junyidao.game.pojo.*;
import com.junyidao.game.service.ChannelService;
import com.junyidao.game.service.CodeService;
import com.junyidao.game.service.GroupService;
import com.junyidao.game.service.UserService;
import com.junyidao.game.vo.TChannelVo;
import com.junyidao.game.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;


/**
 * Created by zzh on 2016/7/6.
 */
@Controller
@RequestMapping("/group")
public class GroupController extends BaseController {

    @Autowired
    private CodeService codeService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private UserService userService;

    @RequestMapping
    @Action(desc = "进入群组管理页面")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("group/list");
        List<PCode> groupLevel = codeService.select("GroupLevel");
        List<PCode> groupType = codeService.select("GroupType");
        result.addObject("groupLevel", groupLevel);
        result.addObject("groupType", groupType);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    @Action(desc = "获取群组管理列表", sysPriv = false)
    public Map<String, Object> listData(TGroupVo vo) {
        Map<String, Object> list = groupService.selectGroup(vo);
        return resultSucess(list);
    }

    @RequestMapping(value = "/toAdd")
    @Action(desc = "进入新增群组页面")
    public ModelAndView add() {
        ModelAndView result = new ModelAndView("group/add");
        List<PCode> groupLevel = codeService.select("GroupLevel");
        List<PCode> groupType = codeService.select("GroupType");
        result.addObject("groupLevel", groupLevel);
        result.addObject("groupType", groupType);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/activate")
    @Action(operType = OperLogConstant.GROUP_ACTIVATE, desc = "启用失效的群组", needLog = true)
    public Map<String, Object> activate(String ids) {
        groupService.activate(ids, BaseConstant.STATUS_NORMAL);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/disactivate")
    @Action(operType = OperLogConstant.GROUP_DISACTIVATE, desc = "注销启用的群组", needLog = true)
    public Map<String, Object> disactivate(String ids) {
        groupService.activate(ids, BaseConstant.STATUS_DELETE);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    @Action(operType = OperLogConstant.GROUP_ADD, desc = "新增群组", needLog = true)
    public Map<String, Object> add(TGroup group) {
        Integer id = groupService.addGroup(group);
        return resultSucess(id);
    }

    @ResponseBody
    @RequestMapping(value = "/listChannel")
    @Action(desc = "获取渠道列表", sysPriv = false)
    public Map<String, Object> listChannel(TChannelVo channel) {
        Map<String, Object> map = channelService.getAll(channel);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/addChannel")
    @Action(operType = OperLogConstant.GROUP_ADD_CHANNEL, desc = "群组--增加渠道", needLog = true)
    public Map<String, Object> addChannel(String ids, Integer groupId) {
        groupService.addChannel(ids, groupId);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/listUser")
    @Action(desc = "获取账号列表", sysPriv = false)
    public Map<String, Object> listUser(UserVo user) {
        Map<String, Object> map = userService.selectAllByGroup(user);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/addUser")
    @Action(operType = OperLogConstant.GROUP_ADD_USER, desc = "群组--增加账号", needLog = true)
    public Map<String, Object> addUser(String ids, Integer groupId) {
        groupService.addUser(ids, groupId);
        return resultSucess(null);
    }

    @RequestMapping(value = "/detail")
    @Action(desc = "群组详情")
    public ModelAndView detail(Integer groupId, String editFlag) {
        ModelAndView result = new ModelAndView("group/detail");
        List<PCode> groupLevel = codeService.select("GroupLevel");
        List<PCode> groupType = codeService.select("GroupType");
        TGroup group = groupService.selectGroupByGroupId(groupId);
        result.addObject("groupLevel", groupLevel);
        result.addObject("groupType", groupType);
        result.addObject("editFlag", editFlag);
        result.addObject("group", group);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    @Action(operType = OperLogConstant.GROUP_EDIT, desc = "编辑群组", needLog = true)
    public Map<String, Object> update(TGroup group) {
        groupService.updatePartByPrimaryKey(group);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/delUser")
    @Action(operType = OperLogConstant.GROUP_DELETE_USER, desc = "群组--删除账号", needLog = true)
    public Map<String, Object> delUser(String ids, Integer groupId) {
        groupService.delUser(ids, groupId);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/delChannel")
    @Action(operType = OperLogConstant.GROUP_DELETE_CHANNEL, desc = "群组--删除渠道", needLog = true)
    public Map<String, Object> delChannel(String ids, Integer groupId) {
        groupService.deChannel(ids, groupId);
        return resultSucess(null);
    }
}
